# -*- coding:utf-8 -*-
import logging
import os
import time
from configs.setting import FILE_PATH
# 按文件大小滚动备份 RotatingFileHandler 是 Python 自带的日志处理器之一
from logging.handlers import RotatingFileHandler
# 导入模块
import colorlog

logs_path = FILE_PATH['log']
if not os.path.exists(logs_path):
    os.makedirs(logs_path)
logfile_name = logs_path + r'\test.{}.log'.format(time.strftime("%Y%m%d"))


class HandleLogs:

    @classmethod
    def setting_log_color(cls):
        log_color_config = {
            'DEBUG': 'cyan',
            'INFO': 'green',
            'ERROR': 'red',
            'WARNING': 'yellow',
            'CRITICAL': 'red'
        }
        formatter = colorlog.ColoredFormatter(
            '%(log_color)s %(levelname)s - %(asctime)s - %(filename)s:%(lineno)d -[%(module)s:%(funcName)s] - '
            '%(message)s', log_colors=log_color_config)
        return formatter

    @classmethod
    def output_logs(cls):
        logger = logging.getLogger(__name__)
        steam_format = cls.setting_log_color()
        if not logger.handlers:
            logger.setLevel(logging.DEBUG)
            log_format = logging.Formatter(
                '%(levelname)s - %(asctime)s - %(filename)s:%(lineno)d -[%(module)s:%(funcName)s] - %(message)s')
            sh = logging.StreamHandler()
            sh.setLevel(logging.DEBUG)
            sh.setFormatter(steam_format)
            logger.addHandler(sh)

            fh = RotatingFileHandler(filename=logfile_name, mode='a', maxBytes=5242880, backupCount=7, encoding='utf-8')
            fh.setLevel(logging.DEBUG)
            fh.setFormatter(log_format)
            logger.addHandler(fh)
        return logger


handle = HandleLogs()
logs = handle.output_logs()